package com.hikarios.elasticsearch.esjd.controller;

import api.ElasticOperation;
import apitest.ElasticOperationFactory;
import cn.hutool.json.JSONUtil;
import com.hikarios.elasticsearch.esjd.service.ContentService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.*;

@RestController
@RequiredArgsConstructor
@Slf4j
public class ContentController {

    @NonNull
    private ContentService contentService;

    private ElasticOperationFactory eloFactory;

    /**
     * 解析 并存储到es中
     * @param keyword
     * @return
     * @throws IOException
     */
    @GetMapping("/parse/{keyword}")
    public String parse(@PathVariable String keyword) throws IOException {
        Boolean flag = contentService.parseContent(keyword);
        if (flag){
            return "操作成功";
        }else {
            return "操作失败";
        }
    }

    /**
     * 分页搜索
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/search/{keyword}/{pageNum}/{pageSize}")
    public String searchPage(@PathVariable String keyword, @PathVariable Integer pageNum, @PathVariable Integer pageSize) throws IOException {
        log.warn("接受到请求");
        List<Map<String, Object>> list = contentService.searchPage(pageNum, pageSize, keyword);
        return JSONUtil.toJsonStr(list);
    }

    /**
     * 文档全文检索
     * @param keyword
     * @return
     */
    @GetMapping("/searchDocument/{keyword}")
    public String searchPage(@PathVariable String keyword) throws IOException {
        log.warn("接受到请求参数："+keyword);
        List<Map<String, Object>> list = new ArrayList<>();
        SearchHits hits = contentService.getHits(keyword);
        //hits是一个迭代器，所以需要迭代返回每一个hits
        Iterator<SearchHit> iterator = hits.iterator();
        int count = 0;
        while (iterator.hasNext()) {
            Map<String,Object> map = new HashMap();
            map.put("index",count);
            System.out.println(count);
            SearchHit hit = iterator.next();
            //获取返回的字段
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            count++;
            //这个就会把匹配到的文本返回，而且只返回匹配到的部分文本
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            map.put("fileName",sourceAsMap.get("name"));
            String s = String.valueOf(highlightFields.get("attachment.content"));
            String replace = s.replace("[attachment.content]", "").replace(", fragments[[", "").replace("]]","");
            map.put("fileContent_fragments",replace);
            list.add(map);
        }
        return JSONUtil.toJsonStr(list);
    }
}
